from menu . logic import menu_logic

class callbacker_fake :
    def __init__ ( self ) :
        self . _call_history = [ ]
    def first_callback ( self ) :
        self . _call_history += [ callbacker_fake . first_callback ]
    def second_callback ( self ) :
        self . _call_history += [ callbacker_fake . second_callback ]
    def third_callback ( self ) :
        self . _call_history += [ callbacker_fake . third_callback ]

class mediator_fake :
    def __init__ ( self ) :
        self . _call_history = [ ]
        self . _added_table_labels = [ ]
        self . _row_selection_history = [ ]
    def add_table_label ( self , text ) :
        self . _call_history += [ mediator_fake . add_table_label ]
        self . _added_table_labels += [ text ]
    def finish_table_row ( self ) :
        self . _call_history += [ mediator_fake . finish_table_row ]
    def select_table_row ( self , row ) :
        self . _row_selection_history += [ { mediator_fake . select_table_row : row } ]
    def unselect_table_row ( self , row ) :
        self . _row_selection_history += [ { mediator_fake . unselect_table_row : row } ]

class menu_logic_tests :
    'menu_logic'
    def __init__ ( self , test ) :
        self . _test = test
        self . _mediator = mediator_fake ( )
        self . _menu_logic = menu_logic ( self . _mediator )
    def test_show_main_menu_add_title ( self ) :
        self . _menu_logic . configure_title_text_for_menu ( 'my title' )
        self . _menu_logic . show_menu ( )
        self . _test . assume_equal ( self . _mediator . _added_table_labels [ 0 : 1 ] , [ 'my title' ] )
        self . _test . assume_equal ( self . _mediator . _call_history [ 0 : 2 ] , [ mediator_fake . add_table_label , mediator_fake . finish_table_row ] )
    def test_show_main_menu_add_menu_items ( self ) :
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( 'my first row' , None )
        self . _menu_logic . configure_next_row_for_menu ( 'my second row' , None )
        self . _menu_logic . show_menu ( )
        self . _test . assume_equal ( self . _mediator . _added_table_labels [ 1 : 3 ] , 
            [ 'my first row' 
            , 'my second row' 
            ] )
        self . _test . assume_equal ( self . _mediator . _call_history [ 2 : 6 ] , 
            [ mediator_fake . add_table_label 
            , mediator_fake . finish_table_row
            , mediator_fake . add_table_label
            , mediator_fake . finish_table_row 
            ] )
    def test_show_main_menu_add_menu_items_after_hide ( self ) :
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( 'my old first row' , None )
        self . _menu_logic . configure_next_row_for_menu ( 'my old second row' , None )
        self . _menu_logic . show_menu ( )
        self . _menu_logic . hide_menu ( )
        self . _mediator . _added_table_labels = [ ]
        self . _mediator . _call_history = [ ]
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( 'my new first row' , None )
        self . _menu_logic . configure_next_row_for_menu ( 'my new second row' , None )
        self . _menu_logic . show_menu ( )
        self . _test . assume_equal ( self . _mediator . _added_table_labels [ 1 : 3 ] , 
            [ 'my new first row' 
            , 'my new second row' 
            ] )
        self . _test . assume_equal ( self . _mediator . _call_history [ 2 : 6 ] , 
            [ mediator_fake . add_table_label 
            , mediator_fake . finish_table_row
            , mediator_fake . add_table_label
            , mediator_fake . finish_table_row 
            ] )
    def test_show_main_menu_select_first_command_row ( self ) :
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . show_menu ( )
        self . _test . assume_equal ( self . _mediator . _row_selection_history , [ { mediator_fake . select_table_row : 1 } ] )    
    def test_click_on_menu_item_row ( self ) :
        callbacker = callbacker_fake ( )
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( None , callbacker . first_callback )
        self . _menu_logic . show_menu ( )
        self . _menu_logic . on_table_row_click ( 1 )
        self . _test . assume_equal ( callbacker . _call_history , [ callbacker_fake . first_callback ] )    
    def test_click_on_title ( self ) :
        callbacker = callbacker_fake ( )
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( None , callbacker . first_callback )
        self . _menu_logic . show_menu ( )
        self . _menu_logic . on_table_row_click ( 0 )
        self . _test . assume_empty ( callbacker . _call_history )    
    def test_select_clicked_menu_item ( self ) :
        callbacker = callbacker_fake ( )
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( None , callbacker . first_callback )
        self . _menu_logic . configure_next_row_for_menu ( None , callbacker . second_callback )
        self . _menu_logic . show_menu ( )
        self . _mediator . _row_selection_history = [ ]
        self . _menu_logic . on_table_row_click ( 2 )
        self . _test . assume_equal ( self . _mediator . _row_selection_history , 
            [ { mediator_fake . unselect_table_row : 1 }
            , { mediator_fake . select_table_row : 2 }
            ] )
    def test_select_clicked_title ( self ) :
        callbacker = callbacker_fake ( )
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( None , callbacker . first_callback )
        self . _menu_logic . show_menu ( )
        self . _mediator . _row_selection_history = [ ]
        self . _menu_logic . on_table_row_click ( 0 )
        self . _test . assume_empty ( self . _mediator . _row_selection_history )     
    def test_select_key_down ( self ) :
        callbacker = callbacker_fake ( )
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . show_menu ( )
        self . _mediator . _row_selection_history = [ ]
        self . _menu_logic . on_key_down ( )
        self . _test . assume_equal ( self . _mediator . _row_selection_history , 
            [ { mediator_fake . unselect_table_row : 1 }
            , { mediator_fake . select_table_row : 2 }
            ] )    
    def test_select_key_up ( self ) :
        callbacker = callbacker_fake ( )
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . show_menu ( )
        self . _menu_logic . on_key_down ( )
        self . _mediator . _row_selection_history = [ ]
        self . _menu_logic . on_key_up ( )
        self . _test . assume_equal ( self . _mediator . _row_selection_history , 
            [ { mediator_fake . unselect_table_row : 2 }
            , { mediator_fake . select_table_row : 1 }
            ] )    
    def test_select_key_up_bound ( self ) :
        callbacker = callbacker_fake ( )
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . show_menu ( )
        self . _mediator . _row_selection_history = [ ]
        self . _menu_logic . on_key_up ( )
        self . _test . assume_equal ( self . _mediator . _row_selection_history , 
            [ { mediator_fake . unselect_table_row : 1 }
            , { mediator_fake . select_table_row : 3 }
            ] )    
    def test_select_key_down_bound ( self ) :
        callbacker = callbacker_fake ( )
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . show_menu ( )
        self . _menu_logic . on_key_up ( )
        self . _mediator . _row_selection_history = [ ]
        self . _menu_logic . on_key_down ( )
        self . _test . assume_equal ( self . _mediator . _row_selection_history , 
            [ { mediator_fake . unselect_table_row : 3 }
            , { mediator_fake . select_table_row : 1 }
            ] )    
    def test_key_return_on_first_menu_item ( self ) :
        callbacker = callbacker_fake ( )
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( None , callbacker . first_callback )
        self . _menu_logic . show_menu ( )
        self . _menu_logic . on_key_return ( )
        self . _test . assume_equal ( callbacker . _call_history , [ callbacker_fake . first_callback ] )    
    def test_key_return_on_third_menu_item ( self ) :
        callbacker = callbacker_fake ( )
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( None , callbacker . first_callback )
        self . _menu_logic . configure_next_row_for_menu ( None , callbacker . second_callback )
        self . _menu_logic . configure_next_row_for_menu ( None , callbacker . third_callback )
        self . _menu_logic . show_menu ( )
        self . _menu_logic . on_key_down ( )
        self . _menu_logic . on_key_return ( )
        self . _test . assume_equal ( callbacker . _call_history , [ callbacker_fake . second_callback ] )    
    def test_key_down_after_start ( self ) :
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . start ( )
        self . _menu_logic . on_key_down ( )
        self . _test . assume_empty ( self . _mediator . _row_selection_history )    
    def test_key_up_after_start ( self ) :
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . start ( )
        self . _menu_logic . on_key_up ( )
        self . _test . assume_empty ( self . _mediator . _row_selection_history )    
    def test_key_return_after_start ( self ) :
        callbacker = callbacker_fake ( )
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( None , callbacker . first_callback )
        self . _menu_logic . configure_next_row_for_menu ( None , callbacker . second_callback )
        self . _menu_logic . start ( )
        self . _menu_logic . on_key_return ( )
        self . _test . assume_empty ( callbacker . _call_history )    
    def test_key_down_after_hide ( self ) :
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . show_menu ( )
        self . _menu_logic . hide_menu ( )
        self . _mediator . _row_selection_history = [ ]
        self . _menu_logic . on_key_down ( )
        self . _test . assume_empty ( self . _mediator . _row_selection_history )    
    def test_key_up_after_hide ( self ) :
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . configure_next_row_for_menu ( None , None )
        self . _menu_logic . show_menu ( )
        self . _menu_logic . hide_menu ( )
        self . _mediator . _row_selection_history = [ ]
        self . _menu_logic . on_key_up ( )
        self . _test . assume_empty ( self . _mediator . _row_selection_history )    
    def test_key_return_after_hide ( self ) :
        callbacker = callbacker_fake ( )
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( None , callbacker . first_callback )
        self . _menu_logic . show_menu ( )
        self . _menu_logic . hide_menu ( )
        self . _menu_logic . on_key_return ( )
        self . _test . assume_empty ( callbacker . _call_history )    
    def test_table_row_click_after_hide ( self ) :
        callbacker = callbacker_fake ( )
        self . _menu_logic . configure_title_text_for_menu ( None )
        self . _menu_logic . configure_next_row_for_menu ( None , callbacker . first_callback )
        self . _menu_logic . show_menu ( )
        self . _menu_logic . hide_menu ( )
        self . _menu_logic . on_table_row_click ( 1 )
        self . _test . assume_empty ( callbacker . _call_history )    
